package com.mlingdu.ffmpegaudioplayer.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mlingdu.ffmpegaudioplayer.R;
import com.mlingdu.ffmpegaudioplayer.tester.AudioCaptureTester;
import com.mlingdu.ffmpegaudioplayer.tester.AudioCodecTester;
import com.mlingdu.ffmpegaudioplayer.tester.NativeAudioTester;
import com.mlingdu.ffmpegaudioplayer.tester.Tester;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.widget.Toast.LENGTH_SHORT;

/**
 * Created by daimaren on 2018/1/26
 * 975808394@qq.com
 * 主界面，通过主界面点击进入各个子界面
 */

public class MainActivity extends AppCompatActivity {
	private static final String TAG = MainActivity.class.getSimpleName();
	@BindView(R.id.grid)
	GridView mGridView;
	private adapter mAdapter;
	private Tester mTester;
	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ButterKnife.bind(this);
		initView();
	}

	private void initView() {
		//init adapter
		mAdapter = new adapter();
		mGridView.setAdapter(mAdapter);
	}

	public class adapter extends BaseAdapter {

		private static final int TILES_COUNT = 8;

		private final int[] DRAWABLES = {
				R.drawable.bg_blue,
				R.drawable.bg_green,
				R.drawable.bg_purple,
				R.drawable.bg_yellow
		};

		@Override
		public int getCount() {
			return TILES_COUNT;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			RelativeLayout v;
			if (convertView == null) {
				v = (RelativeLayout) getLayoutInflater().inflate(R.layout.item_grid, parent, false);
			} else {
				v = (RelativeLayout) convertView;
			}
			v.setBackgroundResource(DRAWABLES[position % 5]);

			TextView textView1 = (TextView) v.findViewById(R.id.textView1);
			TextView textView2 = (TextView) v.findViewById(R.id.textView2);

			String string1 = "", string2 = "";
			if(position == 0) {
				string1 = "绘制一张图片";
				string2 = "SurfaceView";
			} else if(position == 1) {
				string1 = "播放本地TS码流";
				string2 = "TS Demux";
			} else if(position == 2) {
				string1 = "H264封装成TS";
				string2 = "TS Remux";
			} else if(position == 3) {
				string1 = "录制 wav 文件";
				string2 = "AudioCapturer";
			}
			else if(position == 4) {
				string1 = "播放 wav 文件";
				string2 = "AudioPlayer";
			}
			else if(position == 5) {
				string1 = "OpenSL ES录制";
				string2 = "OpenSL ES";
			}
			else if(position == 6) {
				string1 = "OpenSL ES播放";
				string2 = "OpenSL ES";
			}
			else if(position == 7) {
				string1 = "音频编解码";
				string2 = "AudioDecoder";
			}
			textView1.setText(string1);
			textView2.setText(string2);

			final int currentPosition = position;
			v.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if(currentPosition == 0) {
						goToImageShowActivity();
					} else if(currentPosition == 1) {
						goToTSPlayerActivity();
					} else if(currentPosition == 2) {

					} else if(currentPosition == 3) {
						mTester = new AudioCaptureTester();
					} else if(currentPosition == 4) {
						mTester = new NativeAudioTester(true);
					} else if(currentPosition == 5) {
						mTester = new NativeAudioTester(false);
					} else if(currentPosition == 6) {
						mTester = new AudioCodecTester();
					} else if(currentPosition == 7) {

					}
					if (mTester != null) {
						mTester.startTesting();
						Toast.makeText(getApplication(),"Start Testing !",LENGTH_SHORT).show();
					}
				}
			});
			return v;
		}
	}
	private void goToImageShowActivity(){
		Intent intent = new Intent(this,ImageShowActivity.class);
		startActivity(intent);
	}

	private void goToTSPlayerActivity(){
		Intent intent = new Intent(this,VideoListActivity.class);
		startActivity(intent);
	}
}
